package org.trajectory.web.rest.resources;

import java.sql.SQLException;
import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.trajectory.dao.TrajectoryLoader;
import org.trajectory.data.Line;
import org.trajectory.data.TrajectorySequence.TrajectorySequenceGroupByTime;

@Component
@Path("/trajectory/{datasource}/{shop}/trajectory/query")
public class QueryTrajectory {
	private TrajectoryLoader trajectoryLoader;
	
	
	@Autowired
	public void setTrajectoryLoader(TrajectoryLoader trajectoryLoader) {
		this.trajectoryLoader = trajectoryLoader;
	}


	@GET
	@Produces({"application/json"})
	@Path("{trajectory}/{from}/{to}")
	public List<TrajectorySequenceGroupByTime> getTrajectories(
			@PathParam("datasource") String datasource,
			@PathParam("shop") Integer shop,
			@PathParam("trajectory") String trajectory,
			@PathParam("from") Long from, @PathParam("to") Long to)
			throws Exception{
		trajectoryLoader.download(datasource , from, to, "0".equals(trajectory) ? null : trajectory , shop);
		return trajectoryLoader.getTrajectorySequence();
	}
	
	@GET
	@Produces({"application/json"})
	@Path("lines")
	public List<Line> getLines(@PathParam("datasource") String datasource,
			@PathParam("shop") Integer shop) throws Exception{
		return trajectoryLoader.loadAllLines(datasource, shop);
	}
}
